<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UsersRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            'name'     => 'required|min:2',
            'email'    => 'email',
            'mobile'   => 'required|digits:11|unique:users',
        ];
        if ($this->isMethod('patch')){
            $id = $this->route('user');
            $rules['mobile'] = 'required|digits:11|unique:users,mobile,'.$id;
        }
        return $rules;
    }

    public function messages()
    {
        return [
            'name.required'   => '会员名必填',
            'name.min'        => '会员名至少两个字',
            'email.email'     => 'Email格式错误',
            'mobile.required' => '手机号必填',
            'mobile.digits'   => '手机号必需是11位数字',
            'mobile.unique'   => '手机号不唯一',
        ];
    }
}
